package cn.com.dwr.service.impl;

import cn.com.dwr.common.exception.HubException;
import cn.com.dwr.entity.AppOrder;
import cn.com.dwr.service.*;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dwr.entity.AppRefund;
import cn.com.dwr.mapper.AppRefundMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @author 邓文润
* @description 针对表【app_refund】的数据库操作Service实现
* @createDate 2023-12-12 07:13:55
*/
@Service
public class AppRefundServiceImpl extends ServiceImpl<AppRefundMapper, AppRefund>
    implements AppRefundService{

    @Resource
    AppOrderService appOrderService;

    @Resource
    AppOrderItemService appOrderItemService;

    @Resource
    AppUserService appUserService;

    @Resource
    AppUploadService appUploadService;

    @Override
    public Object getOrderData(long orderId) {
        AppOrder appOrder = appOrderService.getOwnById(orderId);
        appOrder.setOrderItems(appOrderItemService.listByOrderId(orderId));;

        return MapUtil.builder().put("order", appOrder)
                .put("reasons", Arrays.asList("多拍/拍错/不想要了", "不喜欢/效果不好", "少件/发错货/未收到货", "商品与描述不符", "商品质量/故障"))
                .put("methods", Arrays.asList("自行寄回", "上门取货", "仅退款"))
                .build();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object apply(AppRefund appRefund, MultipartFile[] pics) {

        // 1、判断是否可以申请退货
        AppOrder appOrder = appOrderService.getOwnById(appRefund.getOrderId());
        Assert.isTrue(Arrays.asList(1,2,3).contains(appOrder.getOrderStatus()), "该订单不支持退款");

        // 2、根据订单状态区分处理方式、待发货的立即退款、待收货已完成的进入后台处理
        if(appOrder.getOrderStatus() == 1){
            appOrder.setOrderStatus(6);
            appRefund.setRefundStatus(2);

            //TODO 支付宝退款处理
        }else{
            appOrder.setOrderStatus(5);
            appRefund.setRefundStatus(0);
        }

        // 图片处理
        if(pics != null && pics.length > 0){
            try {
                List<String> picUrls = appUploadService.upload(pics, 0.5f, 0.2);
                appRefund.setImages(StrUtil.join(";", picUrls));
            } catch (IOException e) {
                throw new HubException("图片上传失败");
            }
        }

        // 3、更新状态
        appRefund.setOrderSn(appOrder.getSn());
        appRefund.setUserId(appUserService.getCurrentUserId());

        appRefund.setSn(generateRefundSn());
        appRefund.setRefundAmount(appOrder.getTotalAmount());
        appRefund.setCreated(LocalDateTime.now());

        this.save(appRefund);
        appOrderService.updateById(appOrder);

        return null;
    }

    private String generateRefundSn() {
        String dateStr = DateUtil.format(new Date(), "yyMMddHHmm");
        return "T" + dateStr + RandomUtil.randomNumbers(4);
    }
}




